package com.htw.rv.fast_scroller.follow_scroller_bar

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.os.Bundle
import android.view.ViewTreeObserver
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.htw.rv.fast_scroller.R
import com.htw.rv.fast_scroller.adapter.RvAdapter
import com.htw.rv.fast_scroller.databinding.ActivityFastScrollerFollowBinding

/**
 * @author htw
 * createTime: 2022-04-25
 * desc: FloatView 跟随 ScrollerBar, 使用时, 请自行再封装
 */
class FastScrollerFollowActivity : AppCompatActivity() {

    private lateinit var binding: ActivityFastScrollerFollowBinding

    private var mRvAdapter: RvAdapter? = null
    private val mFastScrollerBuilder = FastScrollerBuilder()

    private var llScrollBarWH = IntArray(2)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFastScrollerFollowBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initOnGlobalLayoutListener()
        initViews()
        initRv()
    }

    private fun initViews() {
        binding.llScrollBar.tvScrollTitle.text = ""
    }

    private fun initOnGlobalLayoutListener() {
        binding.llScrollBar.root.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                llScrollBarWH[0] = binding.llScrollBar.root.width
                llScrollBarWH[1] = binding.llScrollBar.root.height
                binding.llScrollBar.root.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }

    private fun initRv() {
        val adapter = RvAdapter().apply {
            initListData()
        }
        mRvAdapter = adapter
        binding.rvContent.layoutManager = LinearLayoutManager(this)
        binding.rvContent.adapter = adapter

        initFastScroller()
    }

    private fun initFastScroller() {
        initFastScrollerBuilder()
        initFastScrollerDrawables(binding.rvContent)
    }

    private fun initFastScrollerBuilder() {
        mFastScrollerBuilder.setFastScrollerStateChangeListener(object :
            FastScrollerStateChangeListener {
            override fun onFastScrollerStateChange(oldState: Int, state: Int) {
                mFastScrollerBuilder.setFastScrollerState(state)
                when (mFastScrollerBuilder.getFastScrollerState()) {
                    FastScrollerState.Visible -> {

                    }
                    else -> {

                    }
                }
            }
        })
        mFastScrollerBuilder.setFastScrollerLocationChangeListener(object :
            FastScrollerLocationChangeListener {
            override fun verticalLocationChange(y: Float) {
                val layoutParams =
                    binding.llScrollBar.root.layoutParams as ConstraintLayout.LayoutParams
                layoutParams.topMargin = (y - llScrollBarWH[1].toFloat() / 2.0F).toInt()
                binding.llScrollBar.root.layoutParams = layoutParams

                val firstVisibleItemPosition =
                    (binding.rvContent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                binding.llScrollBar.tvScrollTitle.text =
                    mRvAdapter?.getItemData(firstVisibleItemPosition)?.name ?: ""
            }

        })
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun initFastScrollerDrawables(recyclerView: RecyclerView) {
        val verticalThumbDrawable: StateListDrawable? =
            this.getDrawable(R.drawable.fast_scroll_horizontal_thumb_drawable) as StateListDrawable?
        val verticalTrackDrawable: StateListDrawable? =
            this.getDrawable(R.drawable.fast_scroll_horizontal_thumb_drawable_trans) as StateListDrawable?
        val horizontalThumbDrawable: StateListDrawable? =
            this.getDrawable(R.drawable.fast_scroll_horizontal_thumb_drawable) as StateListDrawable?
        val horizontalTrackDrawable: StateListDrawable? =
            this.getDrawable(R.drawable.fast_scroll_horizontal_thumb_drawable_trans) as StateListDrawable?

        bindFastScroller(
            recyclerView,
            verticalThumbDrawable,
            verticalTrackDrawable,
            horizontalThumbDrawable,
            horizontalTrackDrawable
        )
    }

    private fun bindFastScroller(
        recyclerView: RecyclerView,
        verticalThumbDrawable: StateListDrawable?,
        verticalTrackDrawable: Drawable?,
        horizontalThumbDrawable: StateListDrawable?,
        horizontalTrackDrawable: Drawable?
    ) {
        require(!(verticalThumbDrawable == null || verticalTrackDrawable == null || horizontalThumbDrawable == null || horizontalTrackDrawable == null)) {
            ("Trying to set fast scroller without both required drawables.")
        }

        FastScrollerCustom(
            mFastScrollerBuilder,
            recyclerView,
            verticalThumbDrawable,
            verticalTrackDrawable,
            horizontalThumbDrawable,
            horizontalTrackDrawable,
            resources.getDimensionPixelSize(androidx.recyclerview.R.dimen.fastscroll_default_thickness),
            resources.getDimensionPixelSize(androidx.recyclerview.R.dimen.fastscroll_minimum_range),
            resources.getDimensionPixelOffset(androidx.recyclerview.R.dimen.fastscroll_margin)
        )
    }
}